package com.hj.security.service;

import java.util.ArrayList;
import java.util.List;

import com.hj.security.dao.AppLockDao;
import com.hj.security.iservice.IService;
import com.hj.security.ui.LockActivity;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.KeyguardManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;

public class WatchDogService extends Service {

	private AppLockDao dao;
	private List<String> apps;
	private ActivityManager activityManager;
	private Intent intent;
	private boolean flag = true;
	// 存放要停止保护的app
	private List<String> stopApps;
	private MyBinder myBinder;
	private SharedPreferences sp;
	// 键盘的管理器
	private KeyguardManager keyguardManager;

	@Override
	public IBinder onBind(Intent intent) {
		return new MyBinder();
	}

	@Override
	public void onCreate() {
		System.out.println("WatchDogService服务创建");
		super.onCreate();
		dao = new AppLockDao(this);
		apps = dao.getAllPackageName();
		stopApps = new ArrayList<String>();
		sp = getSharedPreferences("config", Context.MODE_PRIVATE);
		activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);

		// 注册一个内容观察者
		getContentResolver().registerContentObserver(
				Uri.parse("content://com.hj.security.applockprovider"), false,
				new MyObserver(new Handler()));

		intent = new Intent(this, LockActivity.class);
		// 服务里面是没有任务栈的，所以要指定一个新的任务栈，不然是无法在服务里面启动activity的
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		new Thread() {
			public void run() {
				while (flag) {
					try {
						// 当解锁后，就再一次输入密码
						if(keyguardManager.inKeyguardRestrictedInputMode()){
							stopApps.clear();
						}
						// 得到当前运行的任务栈，参数就是得到多少个任务栈，1就是只拿一个任务栈
						// 1对应的也就是正在运行的任务栈啦
						List<RunningTaskInfo> runningTaskInfos = activityManager
								.getRunningTasks(1);
						// 拿到当前运行的任务栈
						RunningTaskInfo runningTaskInfo = runningTaskInfos
								.get(0);
						// 拿到当前运行的activity的包名
						String packageName = runningTaskInfo.topActivity
								.getPackageName();
						if (apps.contains(packageName)&&!stopApps.contains(packageName)) {
							intent.putExtra("packageName", packageName);
							startActivity(intent);
						}

						sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			};
		}.start();

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		flag = false;
		System.out.println("服务销毁");
	}

	// 临时开启对某个应用的保护
	private void invokeMethodStartApp(String packageName) {
		if (!stopApps.contains(packageName)) {
			stopApps.remove(packageName);
		}
	}

	// 临时停止对某个应用的保护
	private void invokeMethodStop(String packageName) {
		stopApps.add(packageName);
	}

	private class MyObserver extends ContentObserver {

		public MyObserver(Handler handler) {
			super(handler);
			
		}

		@Override
		public void onChange(boolean selfChange) {
			super.onChange(selfChange);
			apps = dao.getAllPackageName();
		}
		
	}

	private class MyBinder extends Binder implements IService {

		@Override
		public void startApp(String packageName) {
			invokeMethodStartApp(packageName);
		}

		@Override
		public void stopApp(String packageName) {
			invokeMethodStop(packageName);
		}

	}
}
